classdef EffectDistortionWindow < handle
    %%EFFECTDISTORTIONWINDOW Dooku Synthesizer Effect Distortion window GUI class
    properties
        % Figure variables
        hfig;
        handles;

        % Instrument data
        Data;
    end
    
    properties (SetObservable)
        finish;
    end
    
    methods
        
        function obj = EffectDistortionWindow
            % Figure Load
            obj.hfig = hgload('EffectDistortionGUI.fig');
            obj.handles = guihandles(obj.hfig);
            
            % Callbacks Definitions
            set(obj.handles.instrumentWindow, 'CloseRequestFcn', @(src, event) finishSelect(obj, src, event));
            set(obj.handles.driveEdit, 'Callback', @(src, event) driveChange(obj, src, event));
            set(obj.handles.driveSlider, 'Callback', @(src, event) driveSlider(obj, src, event));
            set(obj.handles.toneEdit, 'Callback', @(src, event) toneChange(obj, src, event));
            set(obj.handles.toneSlider, 'Callback', @(src, event) toneSlider(obj, src, event));
            
            obj.defaultVariables();
        end
        
        function defaultVariables(obj)
            obj.Data.name = 'Distortion';
            obj.Data.drive = 0.5;
            obj.Data.tone = 0.5;
        end
        
        function visible(obj)
            set(obj.hfig, 'Visible', 'on');
            set(obj.hfig, 'Windowstyle', 'modal');
        end
        
        function refreshWindow(obj, dataIn)
            obj.Data = dataIn;
            set(obj.handles.driveEdit, 'String', num2str(obj.Data.drive));
            set(obj.handles.driveSlider, 'Value', obj.Data.drive);
            set(obj.handles.toneEdit, 'String', num2str(obj.Data.tone));
            set(obj.handles.toneSlider, 'Value', obj.Data.tone);
        end
        
        function instrumentData = saveData(obj)
            
        end
        
        function obj = driveChange(obj, ~, ~)
            driveAux = str2double(get(obj.handles.driveEdit, 'String'));
            if sum(isnan(driveAux)) || sum(driveAux < 0) ||  sum(driveAux > 1)
                errordlg('Drive coeficient must be a numeric value between 0 and 1.', 'Inconsistent error');
                set(obj.handles.driveEdit, 'String', obj.Data.drive);
            else
                obj.Data.drive = driveAux;
                set(obj.handles.driveSlider, 'Value', obj.Data.drive);
            end
        end
        
        function obj = driveSlider(obj, src, ~)
            obj.Data.drive = get(src, 'Value');
            set(obj.handles.driveEdit, 'String', obj.Data.drive);
        end
        
        function obj = toneChange(obj, ~, ~)
            toneAux = str2double(get(obj.handles.toneEdit, 'String'));
            if sum(isnan(toneAux)) || sum(toneAux < 0) ||  sum(toneAux > 1)
                errordlg('Drive coeficient must be a numeric value between 0 and 1.', 'Inconsistent error');
                set(obj.handles.toneEdit, 'String', obj.Data.tone);
            else
                obj.Data.tone = toneAux;
                set(obj.handles.toneSlider, 'Value', obj.Data.tone);
            end
        end
        
        function obj = toneSlider(obj, src, ~)
            obj.Data.tone = get(src, 'Value');
            set(obj.handles.toneEdit, 'String', obj.Data.tone);
        end
        
        function obj = finishSelect(obj, ~, ~)
            obj.finish = 1;
            set(obj.hfig, 'Visible', 'off');
            set(obj.hfig, 'Windowstyle', 'normal');
        end
        
    end
    
end